﻿Public Class frmClientesListado

    Private Sub frmClientesListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgCliente.AutoGenerateColumns = False
        CargarClientes()
    End Sub

    Dim controller As New ControllerSigeco.controller_ventas()

    Private m_frmCliente As frmClientes
    Public Property frmClientesForm() As frmClientes
        Get
            Return m_frmCliente
        End Get
        Set(ByVal value As frmClientes)
            m_frmCliente = value
        End Set
    End Property

    Private m_frmReporte As frmReportes
    Public Property frmRpts() As frmReportes
        Get
            Return m_frmReporte
        End Get
        Set(ByVal value As frmReportes)
            m_frmReporte = value
        End Set
    End Property

    Private Sub CargarClientes()

        Dim dt As New DataTable
        dt = controller.get_Clientes_All(oiml.dbConn, 0, txtProveedor.Text, txtruc.Text, Not cbxHabilitado.Checked)
        dgCliente.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCliente.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgCliente.SelectedRows(0).Cells(0).Value

                If frmClientesForm Is Nothing Then

                    frmClientesForm = New frmClientes
                    frmClientesForm.codCliente = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmClientesForm.ShowDialog()
                    CargarClientes()
                    frmClientesForm.Dispose()
                    frmClientesForm = Nothing
                    'End If
                Else
                    frmClientesForm.codCliente = cod
                    frmClientesForm.ShowDialog()
                    CargarClientes()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarClientes()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProveedor.KeyDown, txtruc.KeyDown, cbxHabilitado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarClientes()

        End If
    End Sub

    Private Sub dgCliente_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgCliente.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgCliente_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCliente.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmClientesForm = New frmClientes()
        frmClientesForm.codCliente = 0
        frmClientesForm.ShowDialog()
        CargarClientes()
    End Sub

    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click

        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "CLIENTES"
            frmRpts.datos_Reporte = CType(dgCliente.DataSource, DataTable).Copy
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "CLIENTES"
                frmRpts.datos_Reporte = CType(dgCliente.DataSource, DataTable).Copy
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "CLIENTES"
                frmRpts.datos_Reporte = CType(dgCliente.DataSource, DataTable).Copy
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "CLIENTESRANKING"
            frmRpts.datos_Reporte = controller.get_Clientes_Ranking(oiml.dbConn)
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "CLIENTESRANKING"
                frmRpts.datos_Reporte = controller.get_Clientes_Ranking(oiml.dbConn)
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "CLIENTESRANKING"
                frmRpts.datos_Reporte = controller.get_Clientes_Ranking(oiml.dbConn)
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If

    End Sub

End Class